<?php

namespace App\Http\Controllers\Api;

use App\Subject;
use App\SubjectPower;
use App\SubjectSort;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SubjectController extends BaseController
{
    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : $this->orderBy;
        $orderType = $request->order_type ? $request->order_type : $this->orderType;
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = Subject::orderBy($orderBy, $orderType);
        if ($request->sort) {
            $lists = $lists->where('sort', $request->sort);
        }
        if ($request->describe) {
            $lists = $lists->where('describe', $request->describe);
        }
        return $lists->with(['sorts', 'describes'])->paginate($pageSize);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function home(Request $request, $sort)
    {
        $user_id = Auth::id();
        $power_status = 1;
        $power = SubjectPower::where('user_id', $user_id)->where('status', 0)->where('subject_sort_id', $sort)->first();
        if (!$power) {
            $power_status = 0;
        }
        $subjectSort = SubjectSort::find($sort);
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        #题目处理 暂时没做  默认没有题目分支
        $lists = Subject::orderBy('id', 'asc')
            ->where('sort', $sort)
            ->with(['describes'])
            ->paginate($pageSize);
        if (!$lists) {
            return response(['message' => '题库不足，敬请期待'], 400);
        }
        return response(['message' => '请答题', 'data' => $lists, 'sort' => $subjectSort, 'status' => $power_status]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * @param \App\Http\Requests\Subject $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function store(\App\Http\Requests\Subject $request)
    {
        if (!SubjectSort::find($request->sort)) {
            return response(['message' => '题目分类不存在'], 400);
        }
        if ($request->sort == 1) {
            $wz = ['N', 'Z', 'Q', 'R', 'W', 'G', 'S', 'P'];
            if (!in_array($request->result_1, $wz) || !in_array($request->result_2, $wz)) {
                return response(['message' => '题目指向错误'], 400);
            }
        }
        if ($request->sort == 2) {
            $wz = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
            if (!in_array($request->result_1, $wz) || !in_array($request->result_2, $wz)) {
                return response(['message' => '题目指向错误'], 400);
            }
        }
        Subject::create($request->all());
        return response(['message' => '添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Subject $subject
     * @return \Illuminate\Http\Response
     */
    public function show(Subject $subject)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Subject $subject
     * @return \Illuminate\Http\Response
     */
    public function edit(Subject $subject)
    {
        //
    }

    /**
     * @param \App\Http\Requests\Subject $request
     * @param Subject $subject
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function update(\App\Http\Requests\Subject $request, Subject $subject)
    {
        if ($request->sort == 1) {
            $wz = ['N', 'Z', 'Q', 'R', 'W', 'G', 'S', 'P'];
            if (!in_array($request->result_1, $wz) || !in_array($request->result_2, $wz)) {
                return response(['message' => '题目指向错误'], 400);
            }
        }
        if ($request->sort == 2) {
            $wz = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
            if (!in_array($request->result_1, $wz) || !in_array($request->result_2, $wz)) {
                return response(['message' => '题目指向错误'], 400);
            }
        }
        $subject->update($request->only(['order', 'subject', 'sort', 'answer_1', 'answer_2', 'result_1', 'result_2']));
        return response(['message' => '修改成功']);
    }

    /**
     * @param Subject $subject
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function destroy(Subject $subject)
    {
        $subject->delete();
        return response(['message' => '删除成功']);
    }
}
